﻿package com.translator.comms
{
    import adobe.utils.CustomActions;
    import com.adobe.serialization.json.JSON;

    /**
     * Class for dealing with State objects
     */
    public class CommStateUtil
    {
        /**
         * Take an object with a bunch of typed values and convert it to a packed-up object
         * we can send over Comm.
         * @param input Incoming object
         * @return Object that is safe to send over Comm
         */
        public static function PackObject(input:Object):Object
        {
            if (null != input)
            {
                var packed:Object = new Object();
                for (var key:String in input)
                {
                    packed[key] = JSON.encode(input[key]);
                }
                return packed;
            }
            return null;
        }

        /**
         * Take a Comm-safe string input and convert it back into a value we are interested in
         * @param input Input string coming from Comm
         * @return A properly typed value
         */
        public static function UnpackValue(input:String):*
        {
            return JSON.decode(input);
        }

        /**
         * Ask the util to update to the latest state if necessary
         * @param comms Comms object to update
         * @param state The comm state
         * @return True if an update was necessary and has been sent, false if not and everything is peachy
         */
        public static function UpdateVersionIfNecessary(comms:IComm, state:ICommState):Boolean
        {
            // The JSON format is not likely to change.
            return false;
        }
    }
}